package org.jeecg.modules.patrolResult.service.impl;

import org.jeecg.modules.patrolResult.entity.AgPatrolResult;
import org.jeecg.modules.patrolResult.entity.AgPatrolResultDetail;
import org.jeecg.modules.patrolResult.mapper.AgPatrolResultDetailMapper;
import org.jeecg.modules.patrolResult.mapper.AgPatrolResultMapper;
import org.jeecg.modules.patrolResult.service.IAgPatrolResultService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 巡检结果
 * @Author: jeecg-boot
 * @Date:   2020-05-26
 * @Version: V1.0
 */
@Service
public class AgPatrolResultServiceImpl extends ServiceImpl<AgPatrolResultMapper, AgPatrolResult> implements IAgPatrolResultService {

	@Autowired
	private AgPatrolResultMapper agPatrolResultMapper;
	@Autowired
	private AgPatrolResultDetailMapper agPatrolResultDetailMapper;
	
	@Override
	@Transactional
	public void saveMain(AgPatrolResult agPatrolResult, List<AgPatrolResultDetail> agPatrolResultDetailList) {
		agPatrolResultMapper.insert(agPatrolResult);
		if(agPatrolResultDetailList!=null && agPatrolResultDetailList.size()>0) {
			for(AgPatrolResultDetail entity:agPatrolResultDetailList) {
				//外键设置
				entity.setPatrolResultId(agPatrolResult.getId());
				agPatrolResultDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(AgPatrolResult agPatrolResult,List<AgPatrolResultDetail> agPatrolResultDetailList) {
		agPatrolResultMapper.updateById(agPatrolResult);
		
		//1.先删除子表数据
		agPatrolResultDetailMapper.deleteByMainId(agPatrolResult.getId());
		
		//2.子表数据重新插入
		if(agPatrolResultDetailList!=null && agPatrolResultDetailList.size()>0) {
			for(AgPatrolResultDetail entity:agPatrolResultDetailList) {
				//外键设置
				entity.setPatrolResultId(agPatrolResult.getId());
				agPatrolResultDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		agPatrolResultDetailMapper.deleteByMainId(id);
		agPatrolResultMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			agPatrolResultDetailMapper.deleteByMainId(id.toString());
			agPatrolResultMapper.deleteById(id);
		}
	}
	
}
